# 定义全局异常处理器
from rest_framework.views import exception_handler
from rest_framework import status
from HomeAutoAI.common.Results import HttpResult  # 确保导入路径正确


def global_exception_handler(exc, context):
    # 调用默认的异常处理器
    response = exception_handler(exc, context)

    if response is not None:
        # 针对已知错误使用默认的处理器返回的 response
        return HttpResult.error(data=response.data, message=response.data.get('detail', 'error'),
                                code=response.status_code)
    # else:
    #     # 针对未知错误，统一返回 500 错误
    #     return HttpResult.error(message="An unexpected error occurred.", code=status.HTTP_500_INTERNAL_SERVER_ERROR)
